<?php
/**
 * DouPHP小程序模块
 * Author: cuihaifeng  qq 366131726 微信同号
 * 网址：www.zbxingke.com www.0533hf.com
 */
if (!defined('IN_DOUCO_API')) {
    die('Access Denied');
}
define('IN_DOUCO', true);
define('API_PATH','api');
$http = ((!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https' ) || (!empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off')) ? 'https://' : 'http://';
define('ROOT_PATH', str_replace(API_PATH . '/include/init.php', '', str_replace('\\', '/', __FILE__)));
define('ROOT_URL', preg_replace('/' . API_PATH . '\//Ums', '', dirname($http . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']) . "/"));
define('LOADER_PATH',ROOT_PATH.API_PATH.'/include/');
define('STARTTIME', microtime());
define('MAGIC_QUOTES_GPC', (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) || @ini_get('magic_quotes_sybase'));
define('TIMESTAMP', time());
define("MEMORY_LIMIT",'256M');

define("COOKIE_PRE",'dou_api');
define("COOKIE_DOMAIN",'');
define("COOKIE_PATH",'/');
define("CACHE",'mysql');
define('DEVELOPMENT',true);


include_once ('../data/config.php');

if(DEVELOPMENT) {
    ini_set('display_errors', '1');
    error_reporting(E_ALL ^ E_NOTICE);
} else {
    error_reporting(0);
}

if(function_exists('date_default_timezone_set')) {
    date_default_timezone_set('PRC');
}

if(function_exists('ini_get') && function_exists('ini_set')) {
    if(@ini_get('memory_limit') != MEMORY_LIMIT) {
        @ini_set('memory_limit',MEMORY_LIMIT);
    }
}


require ROOT_PATH . API_PATH.'/version.inc.php';
require ROOT_PATH . API_PATH.'/include/class/loader.class.php';
load()->func('global');
load()->func('compact');
load()->func('pdo');
load()->func('cache');
load()->model('setting');
load()->library('agent');
load()->func('logging');
define('CLIENT_IP', getip());




require (ROOT_PATH . 'include/smarty/Smarty.class.php');
require (ROOT_PATH . 'include/mysql.class.php');
require (ROOT_PATH . 'include/common.class.php');
require (ROOT_PATH . 'include/action.class.php');
require (ROOT_PATH . 'include/check.class.php');
require (ROOT_PATH . 'include/firewall.class.php');
// 实例化DouPHP核心类
$dou = new Action($dbhost, $dbuser, $dbpass, $dbname, $prefix, DOU_CHARSET);
$check = new Check();
$firewall = new Firewall();
// 读取站点信息
$_CFG = $dou->get_config();
$_DISPLAY = unserialize($_CFG['display']); // 显示设置
$_DEFINED = unserialize($_CFG['defined']); // 自定义属性

// 定义DouPHP其它常量
define('M_URL', ROOT_URL . M_PATH . '/');

// 实例化DouPHP核心类
$dou = new Action($dbhost, $dbuser, $dbpass, $dbname, $prefix, DOU_CHARSET);
$check = new Check();
$firewall = new Firewall();
// 系统模块
$_MODULE = $dou->dou_module();
// 载入语言文件
foreach ((array) $_MODULE['lang'] as $lang_file) {
    require ($lang_file);
}
$_LANG['copyright'] = preg_replace('/d%/Ums', $_CFG['site_name'], $_LANG['copyright']);



$_W = $_GPC = array();


$_W['timestamp'] = TIMESTAMP;
$_W['charset'] = DOU_CHARSET;
$_W['clientip'] = CLIENT_IP;
$_W['isajax'] = isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
$_W['ispost'] = $_SERVER['REQUEST_METHOD'] == 'POST';
$_W['sitescheme'] = $_W['ishttps'] ? 'https://' : 'http://';
$_W['script_name'] = htmlspecialchars(scriptname());


if(MAGIC_QUOTES_GPC) {
    $_GET = istripslashes($_GET);
    $_POST = istripslashes($_POST);
    $_COOKIE = istripslashes($_COOKIE);
}
$cplen = strlen(COOKIE_PRE);
foreach($_COOKIE as $key => $value) {
    if(substr($key, 0, $cplen) == COOKIE_PRE) {
        $_GPC[substr($key, $cplen)] = $value;
    }
}
unset($cplen, $key, $value);
$_GPC = array_merge($_GET, $_POST, $_GPC);
$_GPC = ihtmlspecialchars($_GPC);
if(!$_W['isajax']) {
    $input = file_get_contents("php://input");
    if (!empty($input)) {
        $__input = @json_decode($input, true);
        if (!empty($__input)) {
            $_GPC['__input'] = $__input;
            $_W['isajax'] = true;
        }
    }
    unset($input, $__input);
}

setting_load();
$_W['os'] = Agent::deviceType();
if($_W['os'] == Agent::DEVICE_MOBILE) {
    $_W['os'] = 'mobile';
} elseif($_W['os'] == Agent::DEVICE_DESKTOP) {
    $_W['os'] = 'windows';
} else {
    $_W['os'] = 'unknown';
}
$_W['container'] = Agent::browserType();
if(Agent::isMicroMessage() == Agent::MICRO_MESSAGE_YES) {
    $_W['container'] = 'wechat';
} elseif ($_W['container'] == Agent::BROWSER_TYPE_ANDROID) {
    $_W['container'] = 'android';
} elseif ($_W['container'] == Agent::BROWSER_TYPE_IPAD) {
    $_W['container'] = 'ipad';
} elseif ($_W['container'] == Agent::BROWSER_TYPE_IPHONE) {
    $_W['container'] = 'iphone';
} elseif ($_W['container'] == Agent::BROWSER_TYPE_IPOD) {
    $_W['container'] = 'ipod';
} else {
    $_W['container'] = 'unknown';
}
$controller = $_GPC['c'];
$action = $_GPC['a'];
$do = $_GPC['do'];
header('Content-Type: text/html; charset=' . $_W['charset']);

?>